package com.naihe.anderson.sms.service;

import java.util.ArrayList;
import java.util.List;

import android.app.Service;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.IBinder;
import android.os.Vibrator;

import com.naihe.anderson.sms.activity.SreenShotActivity;
import com.naihe.anderson.sms.application.NhSmsApp;
import com.naihe.anderson.sms.entity.SignatureMsg;
import com.naihe.anderson.sms.manager.SignatureMsgMgr;
import com.naihe.anderson.sms.util.FileUtil;

public class NhSmsMainService extends Service {

	NhSmsApp nhSmsApp;
	SignatureMsgMgr signatureMsgMgr;
	
	private SensorManager mSensorManager;
	private SensorEventListener eventListener;
	private Vibrator vibrator;
	
	@Override
	public void onCreate() {
		super.onCreate();
		
		FileUtil.addLog("NhSmsMainService::onCreate()", NhSmsMainService.class, "NhSmsMainService", null);
		nhSmsApp = (NhSmsApp) getApplication();
		signatureMsgMgr = nhSmsApp.getSignatureMsgMgr();
		nhSmsApp.getRecordMoneyMgr().initRecordMoney();
		
		initSensorManager();
        
		initPendingIntent();
	}
	
	private void initSensorManager(){
		vibrator = (Vibrator) getSystemService(Service.VIBRATOR_SERVICE);
		//获取传感器管理服务
        mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        eventListener = new SensorEventListener() {
			
			@Override
			public void onSensorChanged(SensorEvent event) {
				int sensorType = event.sensor.getType();
				//values[0]:X轴，values[1]：Y轴，values[2]：Z轴
				float[] values = event.values;
				if(sensorType == Sensor.TYPE_ACCELEROMETER){
					/*因为一般正常情况下，任意轴数值最大就在9.8~10之间，只有在你突然摇动手机
					 * *的时候，瞬时加速度才会突然增大或减少。
					 * *所以，经过实际测试，只需监听任一轴的加速度大于14的时候，改变你需要的设置
					 * *就OK了~~~
					 * */
					if((Math.abs(values[0])>14||Math.abs(values[1])>14||Math.abs(values[2])>14)){
						//摇动手机后
						if(nhSmsApp.getActivity() != null && !nhSmsApp.isSreenShot()){
							Intent intent = new Intent(nhSmsApp, SreenShotActivity.class);
							intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
							nhSmsApp.startActivity(intent);
							//摇动手机后，再伴随震动提示~~
							vibrator.vibrate(200);
						}
					}
				}
			}
			
			@Override
			public void onAccuracyChanged(Sensor sensor, int accuracy) {
				//当传感器精度改变时回调该方法，Do nothing.
			}
		};
        //加速度传感器
        mSensorManager.registerListener(eventListener, 
        mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), 
        //还有SENSOR_DELAY_UI、SENSOR_DELAY_FASTEST、SENSOR_DELAY_GAME等，
        //根据不同应用，需要的反应速率不同，具体根据实际情况设定
        SensorManager.SENSOR_DELAY_NORMAL);
	}
	
	private void initPendingIntent(){
		List<SignatureMsg> signatureMsgs = new ArrayList<SignatureMsg>();
		signatureMsgs.addAll(signatureMsgMgr.getSignatureMsgInfo());
		for(SignatureMsg msg : signatureMsgs){
			if(msg.getSignatureMsgStatut() == SignatureMsg.SIGNATUREMSG_STATUT_COMPLETE){
				long currentTime = System.currentTimeMillis();
				if(currentTime < msg.getSignatureMsgTime()){
					signatureMsgMgr.setAlarmManager(msg.getSignatureMsgId(), msg.getSignatureMsgTime());
				} else if(currentTime == msg.getSignatureMsgTime()){
					nhSmsApp.getNhSmsMgr().sendSmsBySignatureMsg(msg);
					msg.setSignatureMsgStatut(SignatureMsg.SIGNATUREMSG_STATUT_SEND);
					signatureMsgMgr.updateSignatureMsgInfo(msg, false);
				} else {
					msg.setSignatureMsgStatut(SignatureMsg.SIGNATUREMSG_STATUT_FAILED);
					signatureMsgMgr.updateSignatureMsgInfo(msg, false);
				}
			}
		}
	}
	
	@Override
	public void onDestroy() {
		if(mSensorManager != null){
			mSensorManager.unregisterListener(eventListener);
			vibrator.cancel();
			eventListener = null;
			vibrator = null;
			mSensorManager = null;
		}
		((NhSmsApp)getApplication()).getNhSmsMgr().deinit();
		FileUtil.addLog("NhSmsMainService::onDestroy()", NhSmsMainService.class, "NhSmsMainService", null);
	}
	
	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}
}
